package com.echat.serviceapigateway.security.controller.admin;

import com.echat.serviceapigateway.security.common.HttpResult;
import com.echat.serviceapigateway.security.common.LayUIResult;
import com.echat.serviceapigateway.security.model.authorization.Resource;
import com.echat.serviceapigateway.security.model.authorization.Service;
import com.echat.serviceapigateway.security.model.authorization.vo.ResourceVo;
import com.echat.serviceapigateway.security.service.ResourceService;
import com.echat.serviceapigateway.security.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by chenmohan on 2018/09/18
 *
 * @description
 */
@RestController
@RequestMapping("/admin/interface")
public class InterfaceController {

    @Autowired
    ResourceService resourceService;

    @Autowired
    ServiceService serviceService;

    @RequestMapping("/list")
    public HttpResult<List> listVoByCondition(Resource resource) {
        return LayUIResult.withPageInfo(resourceService.listVoByCondition(resource));
    }

    @RequestMapping("/allService")
    public HttpResult<Service> listServices() {
        return HttpResult.success(serviceService.listAll());
    }

    @RequestMapping("/add")
    public HttpResult<Void> addInterface(Resource resource) {
        resourceService.add(resource);
        return HttpResult.success();
    }

    @RequestMapping("/get")
    public HttpResult<ResourceVo> getVoById(Long id) {
        return HttpResult.success(resourceService.getVoById(id));
    }

    @RequestMapping("/modify")
    public HttpResult<Void> modify(Resource resource) {
        resourceService.update(resource);
        return HttpResult.successWithMsg("修改成功");
    }

    @RequestMapping("/delete")
    public HttpResult<Void> delete(Resource resource) {
        resourceService.remove(resource);
        return HttpResult.successWithMsg("删除成功");
    }
}
